<?php

namespace backend\models\c2\search\rest;

use backend\models\c2\entity\BeUser;
use Yii;
use yii\data\ActiveDataProvider;

/**
 * BeUser represents the model behind the search form about `common\models\c2\entity\BeUser`.
 */
class BeUserSearch extends BeUser
{

    public function fields()
    {
        return [
            'id', 'username', 'email', 'mobile_number', 'blocked_at', 'last_login_ip',
            'last_login_at',
        ];
    }

    public function attributeLabels()
    {
        return [
            'id' => Yii::t('app.c2', 'ID'),
            'username' => Yii::t('app.c2', 'Username'),
            'email' => Yii::t('app.c2', 'Email'),
            'mobile_number' => Yii::t('app.c2', 'mobile_number'),
            'blocked_at' => Yii::t('app.c2', 'Blocked At'),
            'last_login_at' => Yii::t('app.c2', 'Last Login At'),
            'last_login_ip' => Yii::t('app.c2', 'Last Login Ip'),
        ];
    }

    /**
     * Creates data provider instance with search query applied
     *
     * @param array $params
     *
     * @return ActiveDataProvider
     */
    public function search($params)
    {
        $query = self::find()->active();
        // $query = self::find();

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
            'sort' => [
                'sortParam' => $this->getSortParamName(),
            ],
            'pagination' => [
                'pageParam' => $this->getPageParamName(),
                'pageSize' => 20,
            ],
        ]);

        $this->load($params);

        if (!$this->validate()) {
            // uncomment the following line if you do not want to return any records when validation fails
            // $query->where('0=1');
            return $dataProvider;
        }

        $query->andFilterWhere([
            'id' => $this->id,
            'status' => $this->status,
            'position' => $this->position,
            'created_at' => $this->created_at,
            'updated_at' => $this->updated_at,
        ]);

        $query->andFilterWhere(['like', 'username', $this->username])
            ->andFilterWhere(['like', 'mobile_number', $this->mobile_number])
            ->andFilterWhere(['like', 'email', $this->email]);

        return $dataProvider;
    }

    public function getPageParamName($splitor = '-'){
        // $name = "BeMenuPage";
        // return \Yii::$app->naming->toSplit($name);
        return 'page';
    }

    public function getSortParamName($splitor = '-'){
        // $name = "BeMenuSort";
        // return \Yii::$app->naming->toSplit($name);
        return 'sort';
    }

}
